package com.elong.android.specialhouse.utils.filter;

import android.text.TextUtils;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 类型功能说明描述
 *
 * @date 2015/12/8 20:38
 * @author cheng.wang
 */
public class BaseFilter {
    private static final String SPECIAL_STR = "[\\-\\(\\)\\*\\s\\.\\|\\+\\?\\[\\]\"\'!\\\\<>/《》‘“、,^~`&$？：@;:·￥#%=（）…_—。，]*";
    private static final String DEFAULT_REGEX ="([0-9][\\-\\(\\)\\*\\s\\.\\|\\+\\?\"\'!\\\\<>/\\[\\]\\{\\}《》‘“、,^~`&$？：@;:·￥#%=（）…－_—。，１２３４５６７８９０｀＝［］＼；＇．／＊＋～！＠＃＄％＾＆＿｛｝｜＂＜＞]*){6,18}[0-9]";
//    private static final String DEFAULT_REGEX ="([0-9]\\D*){7,18}[0-9]";
//        private static final String ID_REGEX = "[1-9]\\d{5}[1-9]\\d{3}((0\\d)|(1[0-2]))(([0|1|2]\\d)|3[0-1])\\d{3}([0-9]|X|x)";
        private static final String ID_REGEX = "[^0-9]+\\d{18}[^0-9]+|^[0-9]\\d{16}[0-9]$|^[0-9]\\d{17}[^0-9]+|[^0-9]+\\d{17}[0-9]$";

    public static final String[] MALICIOUS_TRANSACTIONS_WORDS = {"微信", "WEIXIN", "WECHAT", "QQ", "扣扣", "叩叩", "支付宝", "ALIPAY",
            "ZHIFUBAO", "信用卡", "银行卡", "CREDITCARD", "XINYONGKA", "YINHANGKA"};

    protected ArrayList<String> mSensitiveWords;
    protected String mContent;
    protected BaseFilter mFilter;
    protected String mRegex;
    public static final String REPLACE_WORDS = "***";

    public BaseFilter(String content){
        this.mContent = content;
        init();
    }

    public BaseFilter(BaseFilter filter){
        this.mFilter = filter;
        init();
    }

    public BaseFilter(BaseFilter filter,String content){
        this.mContent = content;
        this.mFilter = filter;
        init();
    }
    private void init() {
        mSensitiveWords = new ArrayList<String>(Arrays.asList(MALICIOUS_TRANSACTIONS_WORDS));
    }

    public void setRegex(String regex) {
        this.mRegex = regex;
    }

    public ArrayList<String> getSensitiveWords() {
        return mSensitiveWords;
    }

    public void addSensitiveWord(String word){
        mSensitiveWords.add(word);
    }

    /**
     * @return 返回用*替换之后的字符串，若返回空，则表示不含敏感词
     */
    public   boolean containsSensitiveWords() {
        // 英文统一转为大写
        String str = mContent.replace(" ", "").toUpperCase();
        for (String word : mSensitiveWords) {
            if (str.contains(word)) {
                return true;
            }
        }
        return false;
    }

    /**
     * @return 是否包含敏感数字
     */
    public  boolean containsMaliciousNums( ) {
        if (!TextUtils.isEmpty(mContent.replace(" ", ""))) {
            //屏蔽手机号（港澳台）银行卡号/座机和qq（7到10位qq）
            Pattern pattern = Pattern.compile(DEFAULT_REGEX);
            Matcher matcher = pattern.matcher(mContent.replace(" ", ""));
            while (matcher.find()) {
                return true;
            }
        }
        return false;
    }

    /**
     * @return 过滤敏感数字
     */
    public String execute() {
        if (!TextUtils.isEmpty(mContent)) {
            //屏蔽特殊手机号（含港澳台手机号），座机号,qq号，特殊银行卡号
            mContent = doRegex(mContent, DEFAULT_REGEX);
        }
        return mContent;
    }

    /**
     * @param source
     * @param regex
     * @return
     */
    protected String doRegex(String source, String regex) {
        Pattern pattern = null;
        Matcher matcher = null;
//        pattern = Pattern.compile(ID_REGEX);
//        matcher = pattern.matcher(source);
//        if (matcher.find()) {
//            String id = matcher.group();
//            int index = source.indexOf(id);
//            String headStr = source.substring(0, index);
//            headStr = doRegex(headStr, regex);
//            String tailStr = source.substring(index + 18, source.length());
//            tailStr = doRegex(tailStr, regex);
//            return  headStr + id + tailStr;
//        }
        pattern = Pattern.compile(regex);
        matcher = pattern.matcher(source);
        if (matcher.find()) {
            String a = matcher.group();
            source = source.replace(a, REPLACE_WORDS);
            source = doRegex(source,regex);
        }
        return source;
    }
}